package com.qkl.xiatang.app.utils;

import com.qkl.xiatang.app.entity.feed.FeedAllocConfig;
import com.qkl.xiatang.app.entity.feed.FeedAllocDetailEntity;
import com.qkl.xiatang.app.utils.appenum.FeedAllocSerialEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.sql.Time;
import java.time.LocalTime;
import java.util.*;

@Slf4j
public class FeedUtils {


    public static FeedAllocDetailEntity findCheckFeedTask(List<FeedAllocDetailEntity> taskList){
        if(ObjectUtils.isEmpty(taskList)){
            return null;
        }
        Date currDate = new Date();
        FeedAllocDetailEntity currTask = new FeedAllocDetailEntity();
        currTask.setStartTime(currDate);

        List<FeedAllocDetailEntity> currTaskList = new ArrayList<>();
        currTaskList.add(currTask);
        currTaskList.addAll(taskList);
        Collections.sort(currTaskList, Comparator.comparing(FeedAllocDetailEntity::getStartTime));

        FeedAllocDetailEntity task = currTaskList.get(0);
        for(int i = 0,size = currTaskList.size(); i < size; i++ ){
            FeedAllocDetailEntity taskEntity = currTaskList.get(i);
            if( i > 0 && taskEntity.getStartTime().compareTo(currDate) == 0 ){
                task = currTaskList.get(i -1);
                break;
            }
        }
        log.info("=========findCheckFeedTask.task={}",task);
        return task;
    }

    public static FeedAllocConfig findFeedConfig(List<FeedAllocConfig> taskList){
        if(ObjectUtils.isEmpty(taskList)){
            return null;
        }
        Time currTime = Time.valueOf(LocalTime.now());
        FeedAllocConfig currTask = new FeedAllocConfig();
        currTask.setStartTime(currTime);

        List<FeedAllocConfig> currTaskList = new ArrayList<>();
        currTaskList.add(currTask);
        currTaskList.addAll(taskList);
        Collections.sort(currTaskList, Comparator.comparing(FeedAllocConfig::getStartTime));

        FeedAllocConfig task = currTaskList.get(0);
        for(int i = 0,size = currTaskList.size(); i < size; i++ ){
            FeedAllocConfig taskEntity = currTaskList.get(i);
            if( i > 0 && taskEntity.getStartTime().compareTo(currTime) == 0 ){
                task = currTaskList.get(i -1);
                break;
            }
        }
        log.info("=========findFeedTask.task={}",task);
        return task;
    }


    @Deprecated
    public static FeedAllocSerialEnum getFeedAllocSerial(){
        int nowHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        if(nowHour < 12){
            return FeedAllocSerialEnum.MORNING;
        }else if( nowHour > 16 ){
            return FeedAllocSerialEnum.NIGHT;
        }
        return FeedAllocSerialEnum.NOON;
    }

}
